<?php 
/**
 * 
 *
 * @author fablezouave
 * @package Mform
 * @since 2007-06-24
 */


/**
 * Main class
 *
 */
class Mform {
	private $method = null;
	private $uri = null;
	private $id = null;
	private $elements = array();
	private $rules = array();
	private $rulesErrors = array();
	private $JSRules = array();
	private $h_elements = array();
	private $hidden_validate = null;

	static public $encoding = 'ISO-8859-1';

	public function __construct($method=null, $uri=null, $id=null, $type=null) {
		$this->setMethod($method);
		$this->hidden_validate = 'validate_Mform_123456';
		$this->addElement('hidden', $this->hidden_validate, '', 1);
		$this->setUri($uri);
	}

	/**
	 * URI definition - form:action
	 *
	 * @param string $uri Action of the form
	 */
	public function setUri($uri) {
		$this->uri = trim($uri);
	}

	public function getUri() {
		return $this->uri;
	}

	public function addElement($type, $name, $label=null, $value=null, $more=array()) {
		$O = null;
		switch(trim(strtolower($type))) {
			case 'text' :
				$this->elements[] = $O = new Mtext($name, $label, $value, $more);
				return $O;
				break;
			case 'hidden' :
				$this->h_elements[] = $O = new Mhidden($name,$value);
				return $O;
				break;
			case 'textarea' :
				$this->elements[] = $O = new Mtextarea($name, $label, $value, $more);
				return $O;
				break;
			case 'date-text' :
				$this->elements[] = $O = new MdateText($name, $label, $value, $more);
				return $O;
				break;
			case 'submit' :
				$this->elements[] = $O = new Msubmit($name, $label, $value, $more);
				return $O;
				break;
		}
	}

	public function display() {
		$r = '';
		$r .= '<form method="'.$this->getmethod().'" action="'.$this->getUri().'">';
		foreach($this->h_elements as $v) {
			$r .= $v->getHTML();
		}
		$r .= '<table>';
		foreach($this->elements as $v) {
			$r .= '<tr>';
			$r .= '<td>' . $v->getLabel() . '</td>';
			$r .= '<td>' . $v->getHTML() . '</td>';
			$r .= '</tr>';
		}
		$r .= '</form>';

		return $r;
	}

	public function setMethod($method) {
		switch(strtolower($method)) {
			case 'post' :
				$this->method  = 'POST';
				break;
			default :
				$this->method = 'GET';
		}
	}

	public function getmethod() {
		return $this->method;
	}

	public function validate() {
		//print_r($_REQUEST);
		if(!empty($_REQUEST[$this->hidden_validate])) {
			/*
			foreach($this->rules as $k => $v) {
			if(!$v) {
			print_r($this->rulesErrors);
			return false;
			}
			}
			*/
			if(!empty($this->rulesErrors)) {
				$re = new sRulesErrors($this->rulesErrors);
				echo $re->render();
				return false;
			}
			return true;
		}
		return false;
	}

	public function addRule($field, $error_message, $rule_type='', $rule_data='', $validate_js='') {
		$O = new sRules($field);

		switch(strtolower($rule_type)) {
			/*
			case 'required' :
				$this->rules[$field] = $validate = $O->validateRequired();

				break;
			case 'email' :
				$this->rules[$field] = $validate = $O->validateEmail();
				break;
			*/
			case 'required' :
			case 'email' :
			case 'integer' :
			case 'bool' :
			case 'numeric' :
				
				$method = 'validate'.ucfirst($rule_type);
				if(!method_exists($O, $method)) {
					throw new MformException('Rule type : '.$rule_type.' does not exists');
					return false;
				}
				$this->rules[$field] = $validate = $O->$method();
				break;
		}
		if($validate_js) {
			$this->JSRules[$field] = $validate_js;
		}
		if(!$validate) {
			$this->rulesErrors[$field] = $error_message;
		}
	}
}// end class

?>